package com.kuang.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.env.Environment;
import org.springframework.core.env.Profiles;
import org.springframework.web.bind.annotation.GetMapping;
import springfox.documentation.RequestHandler;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.service.Contact;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

import java.util.ArrayList;

@Configuration
//开启swagger2
@EnableSwagger2
public class SwaggerConfig {

    //配置多个分组只要配置多个docket即可
    @Bean
    public Docket docket() {
        return new Docket(DocumentationType.SWAGGER_2).groupName("AA");
    }


    //配置了swagger的Docket的Bean实例
    @Bean
    public Docket getDocket(Environment environment) {
        //设置要显示的swagger环境,读取application的配置文件
        Profiles profiles = Profiles.of("dev", "test");
        //通过acceptsProfiles判断是否处在自己设定的环境当中
        boolean b = environment.acceptsProfiles(profiles);

        //
        return new Docket(DocumentationType.SWAGGER_2)
                .apiInfo(apiInfo())
                .select()
                //RequestHandlerSelectors配置要扫描接口的方式
                //basePackage指定要扫描的包
                //withClassAnnotation(GetMapping.class)扫描类上的注解
                //withMethodAnnotation(GetMapping.class)扫描方法上的注解
                .apis(RequestHandlerSelectors.basePackage("com.kuang.controller"))
                //.paths(PathSelectors.ant("/kuang/**"))//过滤
                .build()
                .enable(b)
                .groupName("框");//设置是否启动swagger
    }

    //配置swagger信息=apiInfo
    private ApiInfo apiInfo() {
        Contact contact = new Contact("zs", "sasa", "sa@qq.com");

        return new ApiInfo(
                "ZJ的swagger文档",
                "1ddsad",
                "v1.0.0",
                "http://www.baidu.com",
                contact,
                "apache 2",
                "http://www.baidu.com",
                new ArrayList()
        );
    }
}
